
***************** Balance table (Table 2) **********************************	

		use "$data/tech_cell_input.dta", clear
		keep if cells_50km_stateborders 
				
 		su lang_control [aw=pop2001]				
		replace lang_control=(lang_control-r(mean))/r(sd)		
		
		local covariates logPop ruggedness agriwork_by_totalwork_main dist_nearest_bank median_DIST_TOWN  irr_share_total2001 Lcropsuit_rf_c_low pct_forest share_sc proportion_hindu proportion_muslim literacy_rate cov_share2001 d2cov_share2011 delta_vd_power_all delta_vd_tar_road treatsmip ///
		POWER_ALL BS_FAC EDU_FAC MEDI_FAC POST_OFF TELE_OFF CRSOC_FAC COOP_BANK COMM_FAC dist_closest_agrimkt comp lang_control
		

		foreach var of varlist `covariates'{
			
				reghdfe `var' share_off_speakers sharecrop_all_fao if year == 2006 & cells_50km_stateborders == 1 & dhyv_share2011 !=. & dhyv_share2016 !=.   [aw=pop2001], a(subdist_border_id mca majority_lang_group) cluster(tahsil_id) residuals(r_`var')
				
				replace r_`var' = sqrt(pop2001) * r_`var'
				
		}
		

local num = 1 

local replace replace 

foreach var in logPop ruggedness agriwork_by_totalwork_main dist_nearest_bank median_DIST_TOWN  irr_share_total2001 Lcropsuit_rf_c_low pct_forest share_sc proportion_hindu proportion_muslim literacy_rate cov_share2001 d2cov_share2011 delta_vd_power_all delta_vd_tar_road treatsmip dist_closest_agrimkt comp {
	
	preserve 
	tempfile results 

ols_spatial_HAC r_`var' r_lang_control if year == 2006 & cells_50km_stateborders == 1, lat(latitude) lon(longitude) timevar(year) panelvar(id) dist(10) bartlett dropvar


			regsave using "`results'", `replace' table(col, asterisk(10 5 1) parentheses(stderr) format(%9.3fc))

use "`results'", clear 
			
			drop if var == "N"
			drop if var == "r2"
			
			gen outcome = "`var'"
			gen num = `num'
					
			save "$proc/conley_`var'.dta", replace
			
		
		
		restore
		
		local num = `num' + 1
}

local num = 1 

foreach var in POWER_ALL BS_FAC EDU_FAC MEDI_FAC POST_OFF TELE_OFF CRSOC_FAC COOP_BANK COMM_FAC {
	
	preserve 
	tempfile results 

ols_spatial_HAC r_`var' r_lang_control if year == 2006 & cells_50km_stateborders == 1, lat(latitude) lon(longitude) timevar(year) panelvar(id) dist(10) bartlett dropvar


			regsave using "`results'", `replace' table(col, asterisk(10 5 1) parentheses(stderr) format(%9.3fc))

use "`results'", clear 
			
			drop if var == "N"
			drop if var == "r2"
			
			gen outcome = "`var'"
			gen num = `num'
					
			save "$proc/conley_`var'.dta", replace
			
			
		
		restore
		
		local num = `num' + 1
}



* Append all the RHS columns together 
use "$proc/conley_POWER_ALL.dta", replace 
erase "$proc/conley_POWER_ALL.dta"

foreach var in BS_FAC EDU_FAC MEDI_FAC POST_OFF TELE_OFF CRSOC_FAC COOP_BANK COMM_FAC {
	append using "$proc/conley_`var'.dta"
	erase "$proc/conley_`var'.dta"
}	

rename col col_2 
rename outcome outcome_2 

save "$proc/conley_rhs", replace 

		
* Produce final balance table 
use "$proc/conley_logPop.dta", clear 
erase "$proc/conley_logPop.dta"

foreach var in  ruggedness agriwork_by_totalwork_main dist_nearest_bank median_DIST_TOWN  irr_share_total2001 ///
Lcropsuit_rf_c_low pct_forest share_sc proportion_hindu proportion_muslim literacy_rate cov_share2001 d2cov_share2011 delta_vd_power_all delta_vd_tar_road treatsmip ///
dist_closest_agrimkt comp {
	append using "$proc/conley_`var'.dta"
	erase "$proc/conley_`var'.dta"
}

merge 1:1 var num using "$proc/conley_rhs"
erase "$proc/conley_rhs.dta"

gen stderr = 0 
replace stderr = 1 if strpos(var, "stderr")

sort num stderr , stable 

gen var_2 = var 

replace var = outcome if strpos(var, "coef")
replace var = "" if strpos(var, "stderr")

replace var_2 = outcome_2 if  strpos(var_2, "coef")
replace var_2 = "" if strpos(var_2, "stderr")

order var col var_2 col_2

drop outcome num outcome_2 _merge stderr

replace var = "Log(Population)" if var == "logPop"
replace var = "Ruggedness" if var == "ruggedness"
replace var = "Agri. Workers/Working Pop." if var == "agriwork_by_totalwork_main"
replace var = "Distance to nearest bank (kms)" if var == "dist_nearest_bank"
replace var = "Distance to nearest town (kms)" if var == "median_DIST_TOWN"
replace var = "% Area irrigated (2001)" if var == "irr_share_total2001"
replace var = "Log(crop suitability)" if var == "Lcropsuit_rf_c_low"
replace var = "% Land under forest" if var == "pct_forest"
replace var = "Share scheduled castes population" if var == "share_sc"
replace var = "Male literacy rate (%)" if var == "literacy_rate"
replace var = "% Area under 2G coverage (2001)" if var == "cov_share2001" 
replace var = "$\Delta$ Area under 2G coverage (2001-2011)" if var == "d2cov_share2011"
replace var = "$\Delta$ Access to power grid (2001-2011)" if var == "delta_vd_power_all"
replace var = "$\Delta$ Access to paved roads (2001-2011)" if var == "delta_vd_tar_road"
replace var = "Planned construction of SMIS cell-phone towers" if var == "treatsmip"
replace var = "% Hindu population" if var == "proportion_hindu"
replace var = "% Muslim population" if var == "proportion_muslim"
replace var = "Distance to closest Mandi" if var == "dist_closest_agrimkt"
replace var = "Agri. Market Competition" if var == "comp"
 

replace var_2 = "...power supply" if var_2 == "POWER_ALL"
replace var_2 = "...bus connection" if var_2 == "BS_FAC"
replace var_2 = "...education facility" if var_2 == "EDU_FAC"
replace var_2 = "...medical facility" if var_2 == "MEDI_FAC"
replace var_2 = "...post office" if var_2 == "POST_OFF"
replace var_2 = "...telephone office" if var_2 == "TELE_OFF"
replace var_2 = "...credit society" if var_2 == "CRSOC_FAC"
replace var_2 = "...cooperative bank" if var_2 == "COOP_BANK"
replace var_2 = "...communication facility" if var_2 == "COMM_FAC"

texsave using "$results/tables/table_2.tex", replace dataonly

